package qj.tool.jna;

import java.awt.event.KeyEvent;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

import qj.util.ReflectUtil;
import qj.util.funct.P1;

public class KeyNameUtil {
	public static Map<Integer,String> getKeyNameMap() {
		HashMap<Integer, String> ret = new HashMap<Integer, String>();
		
		ret.putAll(getKeyNameMap_awt());
		ret.putAll(getKeyNameMap_jna());
		
		return ret;
	}

	public static HashMap<Integer, String> getKeyNameMap_jna() {
		HashMap<Integer, String> ret = new HashMap<Integer, String>();
		
		// Get key names from KeyEvent class
		int modifiers = Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL;
		for (Field field : KeyboardHandler.class.getDeclaredFields()) {
			String name = field.getName();
			if (field.getType().equals(Integer.TYPE) && (field.getModifiers()&modifiers) == modifiers) {
				Integer key = ReflectUtil.getFieldValue(field, null);
				String keyName = name.toLowerCase();
				ret.put(key, keyName);
			}
		}
		return ret;
	}

	public static Map<Integer, String> getKeyNameMap_awt() {
		HashMap<Integer, String> ret = new HashMap<Integer, String>();
		
		// Get key names from KeyEvent class
		for (Field field : KeyEvent.class.getDeclaredFields()) {
			String name = field.getName();
			if (name.startsWith("VK_") && field.getType().equals(Integer.TYPE)) {
				Integer key = ReflectUtil.getFieldValue(field, null);
				String keyName = name.substring(3).toLowerCase();
				ret.put(key, keyName);
			}
		}
		
		return ret;
	}
//	public static void main(String[] args) {
//		final Map<Integer, String> keyNameMap = getKeyNameMap();
//		KeyboardHooks.init();
//		KeyboardHooks.createMapOnceP1().e(new P1<Integer>() {public void e(Integer key) {
//			System.out.println(keyNameMap.get(key));
//		}});
//		
//	}
}
